package cn.com.ghy.android.service;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import cn.com.ghy.android.bean.LoginInfoBean;
import cn.com.ghy.android.util.CommonStatic;

public class ImageService {
	public static String sendImage(String picName, String partner,
			String vsdata, LoginInfoBean loginInfo, byte[] imageData)
			throws Exception {

		StringBuffer buffer = null;
		DataOutputStream out = null;
		HttpURLConnection conn = null;
		BufferedReader reader = null;
		
		try {
			String BOUNDARY = "---------------------------198152288819156";
			String urlPath = CommonStatic.URL_IMAGE;
			Map<String, String> contentImage = new HashMap<String, String>();
			contentImage.put("isrealsys", CommonStatic.ISREALSYS);
			contentImage.put("partner", partner);
			contentImage.put("sdate", vsdata);
			contentImage.put("name", picName);
			contentImage.put("id", loginInfo.getPernr());
			StringBuffer sb = new StringBuffer();
			for (Iterator<Map.Entry<String, String>> it = contentImage
					.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, String> e = it.next();
				sb = sb.append("--");
				sb = sb.append(BOUNDARY);
				sb = sb.append("\r\n");
				sb = sb.append("Content-Disposition: form-data; name=\""
						+ e.getKey() + "\"\r\n\r\n");
				sb = sb.append(e.getValue());
				sb = sb.append("\r\n");
			}
			sb = sb.append("--");
			sb = sb.append(BOUNDARY);
			sb = sb.append("\r\n");
			sb = sb.append("Content-Disposition: form-data; name=\"affix\"; filename=\""
					+ picName + "\"\r\n");
			sb = sb.append("Content-Type: Content-Type: image/jpeg\r\n\r\n");
			byte[] data = sb.toString().getBytes("UTF-8");
			byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();
			URL url = new URL(urlPath);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setConnectTimeout(60 * 1000);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type",
					"multipart/form-data; boundary=" + BOUNDARY);
			conn.setRequestProperty(
					"Content-Length",
					String.valueOf(data.length + imageData.length
							+ end_data.length));
			conn.setUseCaches(false);
			conn.connect();
			out = new DataOutputStream(conn.getOutputStream());
			out.write(data);
			out.write(imageData);
			out.write(end_data);
			out.flush();
			out.close();
			reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "utf-8"));
			buffer = new StringBuffer();
			String line = "";
			while ((line = reader.readLine()) != null) {
				buffer.append(line);
			}
			reader.close();
			conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception(e);
		} finally {
			if(null != conn) {
				conn.disconnect();
			}
			if(null != out) {
				out.close();
			}
			if(null != reader) {
				reader.close();
			}
		}
		return buffer.toString();
	}
}
